//Given two strings s and t, return true if t is an anagram of s, and false othe
//rwise. 
//
// 
// Example 1: 
// Input: s = "anagram", t = "nagaram"
//Output: true
// Example 2: 
// Input: s = "rat", t = "car"
//Output: false
// 
// 
// Constraints: 
//
// 
// 1 <= s.length, t.length <= 5 * 104 
// s and t consist of lowercase English letters. 
// 
//
// 
// Follow up: What if the inputs contain Unicode characters? How would you adapt
// your solution to such a case? 
// Related Topics 哈希表 字符串 排序 
// 👍 433 👎 0

package leetcode.editor.cn;

import java.util.HashMap;
import java.util.Map;

class P242ValidAnagram {
    public static void main(String[] args) {
        Solution solution = new P242ValidAnagram().new Solution();
        System.out.println(solution.isAnagram("ab", "ba"));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isAnagram(String s, String t) {
            int len1 = s.length();
            int len2 = t.length();
            if (len1 != len2) {
                return false;
            }
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < len1; i++) {
                map.compute(s.charAt(i), (k, v) -> v == null ? 1 : v + 1);
            }
            for (int i = 0; i < len2; i++) {
                if (!map.containsKey(t.charAt(i)) || map.get(t.charAt(i)) <= 0) {
                    return false;
                } else {
                    map.compute(t.charAt(i), (k, v) -> v - 1);
                }
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}